<!--
 @description 文本输入弹窗
 @fileName index.vue
 @author zengqiongying
 @created 2022/10/30 13:19:38
-->
<template>
  <Popup
    v-model="popShow"
    position="bottom"
    :title="title"
    :show-cancel-button="false"
    @confirm="handleSubmit"
  >
    <van-form ref="form" @submit="handleSubmit">
      <div class="remark-content">
        <van-field
          v-model="content"
          type="textarea"
          row="5"
          label=""
          placeholder="请输入"
          :maxlength="maxlength"
          show-word-limit
          :rules="rules"
        />
      </div>
    </van-form>
  </Popup>
</template>

<script>
import Popup from '@/components/Popup'
export default {
  name: 'PopField',
  components: { Popup },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    value: {
      type: String,
      default: ''
    },
    maxlength: {
      type: Number,
      default: 200
    },
    required: {
      type: Boolean,
      default: false
    },
    rules: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      content: ''
    }
  },

  computed: {
    popShow: {
      set(val) {
        this.$emit('update:show', val)
      },
      get(val) {
        return this.show
      }
    }
  },
  watch: {
    value(val) {
      this.content = val
    }
  },
  created() {
    this.content = this.value || ''
  },
  methods: {
    handleSubmit() {
      if (this.required) {
        this.$refs.form.validate().then(() => {
          const content = this.content.trim()
          this.$emit('input', content)
          this.popShow = false
        })
      } else {
        const content = this.content.trim()
        this.$emit('input', content)
        this.popShow = false
      }
    }
  }
}

</script>
<style lang='less' scoped>
.remark-content{
  padding: 20px 12px 10px;
  border-radius: 12px;
  border: 2px solid #EFF2F7;
  box-sizing: border-box;
  margin-bottom: 16px;
  .van-cell{
    padding: 0;
    font-size: 14px;
    color: #3C4458;
    line-height: 20px;
  }
}
</style>
